/** 主合同详情 */

import ScrollBarLayout from '@/components/ScrollBarLayout';
import Cls from 'classnames';
import { ConractControllerContext } from '../common';
import { useContext, useMemo } from 'react';
import { getDescrConfig, ntkoQueryParams } from '../mconfig';
import { DescriptionsType } from '@/components/DescriptionsType';
import { wordHandChildParam, getKeyRouter } from '@/utils';
import { NtkoPlugin } from '@/common/NtkoPluginType';
import { ContractManageAPI } from '../common/type';
import { DetailInfoViewBottomBtn } from './DetailInfoViewBottomBtn';
import { history } from 'umi';

export const DetailInfoViewTabsMain: React.FC<ContractManageAPI.IDetailInfoViewTabsMain> = ({ onOpenReadOfdWord }) => {
  /** 获取上下文 */
  const { styleFixCls, curRes, locationState } = useContext(ConractControllerContext);

  /** 引入在线预览 */
  const ntkoBrowser = useMemo(() => {
    const { ntkoBrowser } = NtkoPlugin(() => {});
    return ntkoBrowser;
  }, []);

  /** 点击在线预览文件 */
  const onNtkoReadWord = (urlName: string, urlObj: Record<string, any>, onlyRead?: boolean) => {
    const { ntkoWordUrl } = ntkoQueryParams(urlObj, curRes, urlName, onlyRead);

    const childStr = wordHandChildParam(urlObj);

    const extensBool = ntkoBrowser.ExtensionInstalled();
    if (extensBool) {
      ntkoBrowser?.openWindow(ntkoWordUrl, false, '', childStr);
    }
  };

  //拆解参数
  function parseQuery(query) {
    return query.split('&&').reduce((acc, pair) => {
      const [key, value] = pair.split('=');
      acc[key] = value;
      return acc;
    }, {});
  }

  //进入采购页面
  const enterPurchProject = (record) => {
    if (curRes?.['/purchaseSourceController/getBidById']) {
      const purchaseURL = parseQuery(record?.purchaseURL);
      history.push({
        pathname: `/purchaseSourceController/getBidById/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          type: 'detail',
          info: purchaseURL,
          returnPathName: locationState?.pathname,
        },
      });
    }
  };

  /** 获取表单配置 */
  const { config } = getDescrConfig({
    openApplyModal: () => {},
    openTempModalFunc: () => {},
    openSubjectModal: () => {},
    changeTextInfo: {},
    onFormChange: () => {},
    onNtkoReadWord,
    onOpenReadOfdWord,
    openCounterpartModal: () => {},
    setOpenMaterialModal: () => {},
    // setOpenOwnerDeptModal: () => { }
    enterPurchProject,
  });

  return (
    <>
      <div className="commonTabsBodyDom">
        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
          <ScrollBarLayout style={{ padding: '5px 20px' }}>
            <DescriptionsType config={config} />
          </ScrollBarLayout>
        </div>

        <DetailInfoViewBottomBtn position="bottom" />
      </div>
    </>
  );
};
